<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>8.call和apply的用法和区别.html</title>
</head>

<body>
    <script>
        // call和apply都是改变this的指向，bind是返回新的函数示例，并且this指向bind传入的对象,返回函数实例

        // 新版红皮书：call和apply是函数的两个方法，都会以指定的this值来调用函数
        // 传参区别:apply是数组形式的传参,call是参数列表,就是一个个列出来
        var color = "blue"

        function sayColor() {
            console.log(this.color)
        }

        function Obj() {
            this.color = 'red';
            this.sayColor = function() {
                console.log(this.color)
            }
        }

        let obj = new Obj()
        sayColor()
        obj.sayColor()

        sayColor.call(obj)
        sayColor.apply(obj)

        function Bind() {
            this.name = "bind"
            this.sayColor = function() {
                console.log(this.name)
            }
        }

        var bind = new Bind()
        bind.sayColor()
    </script>
</body>

</html>